package net.vkits.common.validators;

import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by ethan-liu on 16/6/7.
 */
public class CellPhoneValidator implements ConstraintValidator<CellPhoneNum, String> {

    @Override
    public void initialize(CellPhoneNum constraintAnnotation) {
        //初始化，得到注解数据
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isEmpty(value)) {
            return true;
        }
        if (isCellPhoneNum(value)) {
            return true;
        }
        return false;
    }

    private static boolean isCellPhoneNum(String cellPhoneNum) {
        Pattern pattern = Pattern.compile("^((13[0-9])|(14[579])|(15[^4,\\D])|(17[678])|(18[0-9]))\\d{8}$");
        Matcher matcher = pattern.matcher(cellPhoneNum);
        return matcher.matches();
    }
}
